package com.example.administrator.kotiln.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.example.administrator.kotiln.R;
import com.example.administrator.kotiln.adapter.MyPagerAdapter;
import com.example.administrator.kotiln.utils.Utility;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/16.
 */

public class ViewPagerActivity extends Activity {
    private ViewPager mPager;
    int position = 50000;
    private boolean isTouched = false;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            mPager.setCurrentItem(position);
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utility.setActionBar(this);
        setContentView(R.layout.activity_viewpager);
        mPager = (ViewPager) findViewById(R.id.main_viewpager);

        //返回
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //构造list集合
        List<Integer> list = new ArrayList<Integer>();
        list.add(R.mipmap.image1);
        list.add(R.mipmap.image2);
        list.add(R.mipmap.image3);
        list.add(R.mipmap.image4);
        MyPagerAdapter adapter = new MyPagerAdapter(this, list);
        mPager.setAdapter(adapter);
        mPager.setCurrentItem(position);
        //实现思路三
        mPager.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                // TODO Auto-generated method stub
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        isTouched = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        isTouched = false;
                    default:
                        break;
                }
                return false;
            }
        });
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                position = arg0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        //实现思路二
        Thread change = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (true) {
                    if (!isTouched) {
                        position++;
                        mHandler.sendEmptyMessage(0);
                    }
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        change.start();
    }
}
